// 位段

#include <stdio.h>

// 位段
typedef struct
{
    // 冒号后面表示占用多少 bit
    unsigned int leading : 3;
    unsigned int FLAG1 : 1;
    unsigned int FLAG2 : 1;
    int trailing : 27;
} U0;

void prtBin(unsigned int number);

int main(int argc, char const *argv[])
{
    U0 uu;
    uu.leading = 2;
    uu.FLAG1 = 0;
    uu.FLAG2 = 1;
    uu.trailing = 0x7ffffff;
    printf("sizeof(uu) = %lu\n", sizeof(uu));
    prtBin(*(int*)&uu);
    
    return 0;
}

void prtBin(unsigned int number)
{
    unsigned mask = 1u << 31;
    for (; mask; mask >>= 1)
    {
        printf("%d", number & mask ? 1 : 0);
    }
    printf("\n");
}

// 位段不具有可移植性
// 长度超过一个 int 会补到 2 个 int